package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.common.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WemetiaMapper;
import com.heima.wemedia.service.WemetiaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;
@Service
public class WemetiaServiceImpl extends ServiceImpl<WemetiaMapper, WmMaterial> implements WemetiaService  {
   @Autowired
   private  FileStorageService storageService;
    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        //检查参数
        if (multipartFile==null)
        {
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_IMAGE_FORMAT_ERROR,"图片未选择");
        }
        //上传文件
        String fileName= UUID.randomUUID().toString().replace("-","");
        String originalFilename = multipartFile.getOriginalFilename();
        String postfix
                = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileId=null;
        try {


            fileId = storageService.uploadImgFile("", fileName + postfix, multipartFile.getInputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //保存到数据库
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(WmThreadLocalUtil.getUser().getId());
        wmMaterial.setUrl(fileId);
        wmMaterial.setIsCollection((short)0);
        wmMaterial.setType((short)0);
        wmMaterial.setCreatedTime(new Date());
        save(wmMaterial);

        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public ResponseResult listPage(WmMaterialDto wmMaterialDto) {
        wmMaterialDto.checkParam();
        IPage page=new Page(wmMaterialDto.getPage(),wmMaterialDto.getSize());
        LambdaQueryWrapper<WmMaterial> wmMaterialLambdaQueryWrapper=new LambdaQueryWrapper<>();
        //是否是查询收藏
        if(wmMaterialDto.getIsCollection()!=null&&wmMaterialDto.getIsCollection()==1)
        {
         wmMaterialLambdaQueryWrapper.eq(WmMaterial::getIsCollection,wmMaterialDto.getIsCollection());
        }
        wmMaterialLambdaQueryWrapper.eq(WmMaterial::getUserId,WmThreadLocalUtil.getUser().getId());
        wmMaterialLambdaQueryWrapper.orderByDesc(WmMaterial::getCreatedTime);
         page = page(page, wmMaterialLambdaQueryWrapper);
        ResponseResult responseResult = new PageResponseResult(wmMaterialDto.getPage(),
                wmMaterialDto.getSize(),
                (int) page.getTotal());

        responseResult.setData(page.getRecords());

        return responseResult;


    }
}
